package com.thread.jucTest;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @program: leetcode
 * @description:
 * @author: wangzhihua
 * @date: 2024-06-25
 */
public class CompletableFutureTest {

	@Test
	public void test1() throws Exception {
		// 同步调用,无返回值
		CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
			System.out.println(Thread.currentThread().getName() + "completableFuture1");
		});
		completableFuture1.get();

		// 异步调用,有返回值
		CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread().getName() + "completableFuture2");
//			int n = 1/0;
			return 1024;
		});
		completableFuture2.whenComplete((t, e) -> {
			System.out.println("----t=" + t); // 正常执行结果,异常时=null
			System.out.println("----e=" + e); // 异常时报的错,正常时=null
		});

	}

}
